* Import initial data and generate state numbers (for state FE)
import delimited "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19742019.csv"
collapse pollib_median, by(st)
gen stateNumber = _n
sort st
save "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta", replace
clear

insheet using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19742019.csv"
sort st
merge st using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta"
drop _merge

* Create TSCS dataset
tsset stateNumber year
* xtset stateNumber year

* Generate custom lagged DVs without using the STATA operators (margins doesn't like it when you try to plot the marginal effects with the lagged operators)

gen ydiff = pollib_median - l.pollib_median
gen ydiff_L1 = l.ydiff

* Set working directory
cd "/Users/nmiras/Desktop/Stata/Partisan Misalignment"

**************************************************
**************************************************

* Unit root tests

xtunitroot llc pollib_median, lags(aic 10)

* Cannot conclude that the panels are stationary

**************************************************
**************************************************

* Create crosstab to show distribution (Table 1)
tab misalignment_scale_factor

* Frequency of unified government at the state level (Figure 1)
tabulate state dem_unified
tabulate state rep_unified

* Average annual change in state policy liberalism (Table 2)
oneway ydiff misalignment_scale_factor if year < 1998, tabulate
oneway ydiff misalignment_scale_factor if year > 1997, tabulate
oneway ydiff misalignment_scale_factor, tabulate 

* Descriptives of new years from Caughey and Warshaw data update
oneway ydiff misalignment_scale_factor if year > 2014, tabulate

* Pairwise comparisons for means

* Full period
pwmean ydiff, over(misalignment_scale_factor) effects

clear 
* Import initial data and generate state numbers (for state FE)
import delimited "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19741997.csv"
collapse pollib_median, by(st)
gen stateNumber = _n
sort st
save "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta", replace
clear

insheet using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19741997.csv"
sort st
merge st using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta"
drop _merge

* Create TSCS dataset
tsset stateNumber year
* xtset stateNumber year

* Generate custom lagged DVs without using the STATA operators (margins doesn't like it when you try to plot the marginal effects with the lagged operators)

gen ydiff = pollib_median - l.pollib_median
gen ydiff_L1 = l.ydiff

* 1975-1997
pwmean ydiff, over(misalignment_scale_factor) effects

clear 
* Import initial data and generate state numbers (for state FE)
import delimited "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19982019.csv"
collapse pollib_median, by(st)
gen stateNumber = _n
sort st
save "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta", replace
clear

insheet using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19982019.csv"
sort st
merge st using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta"
drop _merge

* Create TSCS dataset
tsset stateNumber year
* xtset stateNumber year

* Generate custom lagged DVs without using the STATA operators (margins doesn't like it when you try to plot the marginal effects with the lagged operators)

gen ydiff = pollib_median - l.pollib_median
gen ydiff_L1 = l.ydiff

* 1998-2019
pwmean ydiff, over(misalignment_scale_factor) effects

clear

****************************************************
****************************************************
*
* Main models for paper
*

* Reload full data
* Import initial data and generate state numbers (for state FE)
import delimited "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19742019.csv"
collapse pollib_median, by(st)
gen stateNumber = _n
sort st
save "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta", replace
clear

insheet using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19742019.csv"
sort st
merge st using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta"
drop _merge

* Create TSCS dataset
tsset stateNumber year
* xtset stateNumber year

* Generate custom lagged DVs without using the STATA operators (margins doesn't like it when you try to plot the marginal effects with the lagged operators)

gen ydiff = pollib_median - l.pollib_median
gen ydiff_L1 = l.ydiff

* Set working directory
cd "/Users/nmiras/Desktop/Stata/Partisan Misalignment"

* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) replace dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model1

* Model 2
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if south == 0 

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model2

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model3

* Model 4
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & south == 0

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model4

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model5

* Model 6
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & south == 0

outreg2 using table3.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

margins(b4.i.misalignment_scale_factor), pwcompare(groups)
margins(b4.i.misalignment_scale_factor), post
est store model6

* Create plots

* All years
coefplot model1 model2, keep(*misalignment_scale_factor) ciopts(recast(rcap)) xline(0) scheme(s1mono)

* Early period
coefplot model3 model4, keep(*.misalignment_scale_factor) ciopts(recast(rcap)) xline(0) scheme(s1mono)

* Later period
coefplot model5 model6, keep(*.misalignment_scale_factor) ciopts(recast(rcap)) xline(0) scheme(s1mono)


****************************************************
*
* Robustness check: state-clustered SEs
*
****************************************************

* Full panel (1976-2019)

* Model 1
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union, vce(cluster stateNumber) fe

* Model 2
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if south == 0, vce(cluster stateNumber) fe

* Early period (1976-1997)

* Model 3
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year < 1998, vce(cluster stateNumber) fe

* Model 4
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year < 1998 & south == 0, vce(cluster stateNumber) fe

* Contemporary period (1998-2019)

* Model 5
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year > 1997, vce(cluster stateNumber) fe

* Model 6
xtreg ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year > 1997 & south == 0, vce(cluster stateNumber) fe


****************************************************
*
* For Appendix: full models (showing controls and lags)
*
****************************************************


* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) replace dec(2) pdec(3)

* Model 2
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if south == 0 

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 4
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & south == 0

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 6
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & south == 0

outreg2 using tablea4.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)


****************************************************
*
* For Appendix: model results with no fixed effects
*
****************************************************


* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union

outreg2 using tablea5.xls, replace dec(2) pdec(3)

* Model 2
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if south == 0 

outreg2 using tablea5.xls, append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year < 1998 

outreg2 using tablea5.xls, append dec(2) pdec(3)

* Model 4
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year < 1998 & south == 0

outreg2 using tablea5.xls, append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year > 1997 

outreg2 using tablea5.xls, append dec(2) pdec(3)

* Model 6
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union if year > 1997 & south == 0

outreg2 using tablea5.xls, append dec(2) pdec(3)


****************************************************
*
* For Appendix: model with no controls
*
****************************************************


* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) replace dec(2) pdec(3)

* Model 2
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if south == 0 

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if year < 1998 

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)

* Model 4
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if year < 1998 & south == 0

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if year > 1997 

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)

* Model 6
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if year > 1997 & south == 0

outreg2 using tablea6.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)


****************************************************
*
* For Appendix: Caughey, Warshaw, and Xu (2017) approach
*
****************************************************


* Full panel (1976-2019)

* Model 1
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) replace dec(2) pdec(3)

* Model 2
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if south == 0 

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 4
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & south == 0

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 6
xtpcse pollib_median l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & south == 0

outreg2 using tablea7.xls, keep(l.pollib_median b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)


****************************************************
*
* For Appendix: ordinal DV
*
****************************************************

* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) replace dec(2) pdec(3)

* Model 2
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if south == 0 

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 4
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & south == 0

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 6
xtpcse ydiff l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & south == 0

outreg2 using tablea9.xls, keep(l.ydiff misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)


****************************************************
*
* For Appendix: omitting CA & TX
*
****************************************************


* Full panel (1976-2019)

* Model 1
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) replace dec(2) pdec(3)

* Model 2
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if south == 0 & nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Early period (1976-1997)

* Model 3
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 4
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year < 1998 & south == 0 & nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Contemporary period (1998-2019)

* Model 5
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)

* Model 6
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union i.stateNumber if year > 1997 & south == 0 & nocatx == 0

outreg2 using tablea10.xls, keep(l.ydiff b4.i.misalignment_scale_factor dem_veto_proof rep_veto_proof demmargin bowen_legprof1 citi6013 d.union) append dec(2) pdec(3)


****************************************************
*
* For Appendix: 1936-2019, no controls
*
****************************************************

clear
* Import initial data and generate state numbers (for state FE)
import delimited "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19362019.csv"
collapse pollib_median, by(st)
gen stateNumber = _n
sort st
save "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta", replace
clear

insheet using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/mirasrouse19362019.csv"
sort st
merge st using "/Users/nmiras/Desktop/Stata/Partisan Misalignment/stateNumber.dta"
drop _merge

* Create TSCS dataset
tsset stateNumber year
* xtset stateNumber year

* Generate custom lagged DVs without using the STATA operators (margins doesn't like it when you try to plot the marginal effects with the lagged operators)

gen ydiff = pollib_median - l.pollib_median
gen ydiff_L1 = l.ydiff

* Full panel (all states)
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber

outreg2 using tablea8.xls, keep(l.ydiff b4.i.misalignment_scale_factor) replace dec(2) pdec(3)

* Full panel (non-south)
xtpcse ydiff l.ydiff b4.i.misalignment_scale_factor i.stateNumber if south == 0 

outreg2 using tablea8.xls, keep(l.ydiff b4.i.misalignment_scale_factor) append dec(2) pdec(3)



**************************************************
**************************************************
